# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=atuin
pkgver=15.0.0
pkgrel=1
# Keep in sync with sqlx version in Cargo.lock.
_sqlx_ver=0.6.2
pkgdesc="Magical shell history"
url="https://github.com/ellie/atuin"
# armhf: build getting stuck on CI
# riscv64, s390x: blocked by rust/cargo
# loongarch64: blocked by linux-raw-sys
arch="all !armhf !riscv64 !s390x !loongarch64"
license="MIT"
makedepends="
	cargo
	cargo-auditable
	clang-dev
	jq
	libsodium-dev
	openssl-dev
	sqlite-dev
	"
pkgusers="atuin"
install="
	$pkgname-server.pre-install
	$pkgname.post-install"
subpackages="
	$pkgname-sync
	$pkgname-server
	$pkgname-server-openrc
	$pkgname-zsh-plugin:_zsh_plugin:noarch
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="https://github.com/ellie/atuin/archive/v$pkgver/atuin-$pkgver.tar.gz
	https://github.com/launchbadge/sqlx/archive/v$_sqlx_ver/sqlx-$_sqlx_ver.tar.gz
	fix-sqlx-build.patch
	use-system-tls.patch
	sqlx-use-system-libsqlite.patch
	server-log-without-ansi-style.patch
	getrandom-0.2.10.patch

	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotate
	"
options="!check"  # FIXME: some tests are broken

export SODIUM_USE_PKG_CONFIG=1

prepare() {
	cargo fetch --target="$CTARGET" --locked

	local sqlx_ver
	sqlx_ver=$(cargo metadata --format-version 1 \
		| jq -er '.packages[] | select(.name=="sqlx") | .version') \
		|| { error 'Unable to find sqlx version in Cargo.lock'; return 1; }
	if [ "$_sqlx_ver" != "$sqlx_ver" ]; then
		error "Bump _sqlx_ver in APKBUILD to: $sqlx_ver"
		return 1
	fi

	mkdir -p vendor
	mv "$srcdir"/sqlx-$_sqlx_ver vendor/sqlx

	# Build with sqlx which we patched to remove bundled libsqlite3.
	# See https://github.com/launchbadge/sqlx/issues/191.
	cat >> Cargo.toml <<-EOF

		[patch.crates-io]
		sqlx = { path = "./vendor/sqlx" }
		sqlx-core = { path = "./vendor/sqlx/sqlx-core" }
	EOF

	default_prepare

	cargo fetch --target="$CTARGET"  # update after patching
}

build() {
	msg2 'Building server'
	cargo auditable build --frozen --release --no-default-features --features server
	mv target/release/atuin target/release/atuin-server

	msg2 'Building client with sync'
	cargo auditable build --frozen --release --no-default-features --features client,sync
	mv target/release/atuin target/release/atuin-sync

	msg2 'Building client without sync'
	cargo auditable build --frozen --release --no-default-features --features client

	mkdir -p comp
	local shell; for shell in bash fish zsh; do
		./target/release/atuin gen-completions -s $shell > comp/atuin.$shell
	done
}

check() {
	cargo test --workspace --frozen --features client,sync,server
}

package() {
	pkgdesc="$pkgdesc - offline only client"
	provider_priority=100  # highest

	install -D -m755 target/release/atuin -t "$pkgdir"/usr/bin/
	install -D -m755 target/release/atuin-server -t "$pkgdir"/usr/bin/

	install -D -m644 comp/atuin.bash "$pkgdir"/usr/share/bash-completion/completions/atuin
	install -D -m644 comp/atuin.fish "$pkgdir"/usr/share/fish/vendor_completions.d/atuin.fish
	install -D -m644 comp/atuin.zsh "$pkgdir"/usr/share/zsh/site-functions/_atuin

	install -d -m755 -o atuin "$pkgdir"/etc/$pkgname
	install -D -m640 -o atuin atuin-server/server.toml -t "$pkgdir"/etc/$pkgname/

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname
}

sync() {
	pkgdesc="$pkgdesc - client with history sync"
	provides="$pkgname=$pkgver-r$pkgrel"
	provider_priority=10  # lowest

	install -D -m755 "$builddir"/target/release/atuin-sync "$subpkgdir"/usr/bin/atuin
}

server() {
	pkgdesc="$pkgdesc - sync server"

	amove etc/atuin/server.toml
	amove usr/bin/atuin-server
}

openrc() {
	default_openrc
	install_if="openrc $pkgname-server=$pkgver-r$pkgrel"
}

_zsh_plugin() {
	pkgdesc="$pkgdesc (Zsh plugin)"
	depends="atuin=$pkgver-r$pkgrel"

	install -D -m644 "$builddir"/atuin/src/shell/atuin.zsh \
		"$subpkgdir"/usr/share/zsh/plugins/$pkgname/$pkgname.plugin.zsh
}

sha512sums="
9b3ae7c400707c522750126136d5f95d9ab3e9ab6469557b26783cd02d11e15e5a86a5e7eb50ea6c09c417583f58fff0fe4589ab048893c1ece3cf163201c942  atuin-15.0.0.tar.gz
d5bab0f4ab6843bd8ab3884d0bb4d4166aa0c82b97edcc6134c94fad076d50bf43d24182aa37c23e0e82ea5e78a7db4ef7b6f878e3112b7224865e0ff98d2716  sqlx-0.6.2.tar.gz
c0065aee2dcbf5808024d444bbcf625794ba6f8fcbdbea0c097f4fca2fc929e276b613800416e13b6157fcb13e57aad736632894b9eb72cf024a71cb6831f645  fix-sqlx-build.patch
136ba4414d82fe9bd3f6d6344e606d2b35d8748c89b397fdd69adcb04fe623a1a1c43484801028e976eaa82494d46ad135a46bff28708a26e0b1c442484058c7  use-system-tls.patch
56dad4a6f97a495fa85c2f9ad3bd6a1b684f9a5950079dd57d32e8bde85f7ac14d5efff736ac959c72494d3562d5249c6ef57540f902815bb822eccffc276e93  sqlx-use-system-libsqlite.patch
b76cebe70f46570b93c0100fc46f842ce6bb7bf2f83c13d69178b6eb87996f4717d8491b063f567d9844f70866d4c200e4b77a249c7246e6603cd9d17ebb2de8  server-log-without-ansi-style.patch
a8786f53121df30043e6f6e0ac55d4eb164aef252b6c6fab86921ed77b305dc462e7ff19dc3e2d58dd5d2caf82a6cd567267ad2893be2e4237c7cd556e13d724  getrandom-0.2.10.patch
c3f4e82c7485030bc558821940a46899be6ba1eb0859fa6eb32e4d928f6b455ddeddcc5778a84e0eb9efc1d518f933e4a0d68df195e6a9889ec7bb99da5245c2  atuin.initd
5f1631f0b06f19733025ad3efec6b44ef13937cd8f2f9b7e3cc8c0ba53495850c06fb8c5377d1d003dda00e2d11835056d3fc1b3ba8ecefb575e61a83159f33e  atuin.confd
30946fe22ede072d76331762c862382f03a35735e5741c46367320a116ac285615ae757e20a9cf7adceb8ba24b827581992fa5b7f84e595cd862a3c2ae69dbe0  atuin.logrotate
"
